package io.github.openfeign.cxnotestatistics;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonParser;
import feign.Feign;
import feign.Param;
import feign.RequestLine;
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
import feign.jackson.JacksonDecoder;
import okhttp3.OkHttpClient;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CxNote {

    public interface NoteService {

        @RequestLine("GET /apis/note_note/getNoteListForMicroservice?kw={kw}&puid={puid}&notebookCid={notebookCid}&offsetValue={offsetValue}&pageSize=100")
        public String list(@Param("kw") String kw, @Param("puid") int puid, @Param("notebookCid") String notebookId, int offsetValue);

        @RequestLine("GET /apis/note_note/getOpenedNote?puid={puid}&cid={cid}")
        public String one(@Param("puid") long puid, @Param("cid") String cid);

    }

    static class NoteHeader {
        String cid;
        String title;

        public String getCid() {
            return cid;
        }

        public void setCid(String cid) {
            this.cid = cid;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }
    }

    static class NoteDetail {
        String title;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        String content;
    }

    static Pattern TITLE_PATT = Pattern.compile("(.+?)转码统计");
    static Pattern VIDEO_ALL_PATT = Pattern.compile("视频总量：(\\d+)", Pattern.DOTALL | Pattern.MULTILINE);
    static Pattern AUDIO_ALL_PATT = Pattern.compile("音频总量：(\\d+)", Pattern.DOTALL | Pattern.MULTILINE);
    static Pattern DOC_ALL_PATT = Pattern.compile("文档总量：(\\d+)", Pattern.DOTALL | Pattern.MULTILINE);

    public static void main(String[] args) throws Exception {


        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8089)))
                .build();
        NoteService noteService = Feign.builder()
                .encoder(new GsonEncoder())
//                .decoder(new GsonDecoder())
//                .client(new feign.okhttp.OkHttpClient(okHttpClient))
                .target(NoteService.class, "https://noteyd.chaoxing.com");
        DbOp db = new DbOp();
        for (int page = 1; page < 2; page++) {
            String objs = noteService.list("转码统计", 21025337, "36893fa9-4d6a-43cc-b67f-2a7f93d146b4", page);
            JSONObject json = JSONObject.parseObject(objs);
            JSONObject data = json.getJSONObject("data");
            JSONArray array = data.getJSONArray("list");

            for (int i = 0; i < array.size(); i++) {
                NoteHeader nh = array.getObject(i, NoteHeader.class);
                String detailStr = noteService.one(21025337, nh.getCid());
                JSONObject allObj = JSONObject.parseObject(detailStr);
                JSONObject dataObj = allObj.getJSONObject("data");
                String title = dataObj.getString("title");
                String content = dataObj.getString("content");
//                System.out.println(title + "," + content);
                Matcher ma1 = TITLE_PATT.matcher(title);
                int video_all = 0, audio_all = 0, doc_all = 0;
                String date = "";
                if (ma1.find()) {
                    date = ma1.group(1);
                }
//                System.out.println(content);
                Matcher ma2 = VIDEO_ALL_PATT.matcher(content);
                if (ma2.find()) {
                    video_all = Integer.parseInt(ma2.group(1));
                }
                Matcher ma3 = AUDIO_ALL_PATT.matcher(content);
                if (ma3.find()) {
                    audio_all = Integer.parseInt(ma3.group(1));
                }
                Matcher ma4 = DOC_ALL_PATT.matcher(content);
                if (ma4.find()) {
                    doc_all = Integer.parseInt(ma4.group(1));
                }
                System.out.println(date + "," + video_all + "," + audio_all + "," + doc_all);
                db.insertData(date,video_all,audio_all,doc_all,i);
            }
        }
        db.destroy();
    }

    static class DbOp {
        static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        static final String DB_URL = "jdbc:mysql://10.0.12.24:3306/test";
        static String user = "root";
        static String pass = "1234";
        static Connection conn = null;

        public DbOp() {
            try {
                Class.forName(JDBC_DRIVER);
                conn = DriverManager.getConnection(DB_URL, user, pass);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public void destroy() {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
                System.exit(1);
            }
        }

        public void insertData(String date, int video_all, int audio_all, int doc_all,int i) throws Exception{
            PreparedStatement pstmt = conn.prepareStatement("insert convert_statistics(date,video_all,audio_all,doc_all,insertindex) values(?,?,?,?,?)");
            pstmt.setString(1, date);
            pstmt.setInt(2,video_all);
            pstmt.setInt(3,audio_all);
            pstmt.setInt(4,doc_all);
            pstmt.setInt(5,i);
            pstmt.execute();
            pstmt.close();
        }
    }
}
